
#include "MrpcApplication.h"
#include "user.pb.h"
#include <iostream>
#include "MrpcChannel.h"
#include "MrpcController.h"
int main(int argc, char **argv){
    //整个程序启动后，使用mrpc框架来享受rpc服务调用，一定需要先调用框架的初始化函数
    MrpcApplication::Init(argc, argv);

    //演示调用远程发布的rpc服务 class UserServiceRpc_Stub 给客户端提供服务调用接口
    fixbug::UserServiceRpc_Stub stub(new MrpcChannel());//创建rpc代理对象，需要RpcChannel。
    //Rpc的请求
    fixbug::LoginRequest requset;
    requset.set_name("zhangsan");
    requset.set_pwd("1234556789");
    //Rpc的响应
    fixbug::LoginResponse response;
    //
    MrpcController controller;
    
    stub.Login(&controller,&requset,&response,nullptr);  //通过stub类调用的方法，都是通过RpcChannel.CallMethod()进行调用，传递接口名，方法名，参数，返回值，错误码.

    if(controller.Failed()){
        std::cout << controller.ErrorText() << std::endl;
    }else{
        if(response.result().errcode()){
        std::cout << "rpc.Login response fail "<< response.result().errmsg()<<std::endl;
        }else{
            std::cout << "rpc login response seccess: "<< response.success()<<std::endl;
        }
    }
    fixbug::UserServiceRpc2_Stub stub2(new MrpcChannel());//创建rpc代理对象，需要RpcChannel。
    //  调用计算接口
    fixbug::CaculateRequest request2;
    request2.set_a(10);
    request2.set_b(20);
    request2.set_op("*");
    fixbug::CaculateResponse response2;
    stub2.Caculate(&controller, &request2, &response2, nullptr);

    if(response2.resultcode().errcode()){
        std::cout<< "rpc caculate response fail: "<< response2.resultcode().errmsg()<<std::endl;
    }else {
        std::cout<< "rpc caculate response seccess: "<<response2.result()<<std::endl;
    }
    
    return 0;
}